package com.iflytek.crvoice2txt.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.iflytek.crvoice2txt.R;
import com.iflytek.crvoice2txt.activity.MainActivity;
import com.iflytek.crvoice2txt.entity.RecorderEntity;
import com.iflytek.crvoice2txt.util.FileUtil;
import com.iflytek.crvoice2txt.widget.VoiceView;

import java.util.List;

import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class SignItemAdapter extends BaseAdapter {

    private OnItemClickListener onItemClickListener;
    private List<RecorderEntity> list;
    private Context context;
    private LayoutInflater mInflate;
    private Animation circle_anim;

    public void setOnSaveBtnClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {

        void onSaveBtnClick(int position, String str);

        void onStartPlayAudio(int position);

        void onStopPlayAudio();

        void changeList(String text,int position);

        void onEditTextDataChanged(int position,String text);
    }

    public SignItemAdapter(List<RecorderEntity> list, Context context) {
        this.list = list;
        this.context = context;
        mInflate = LayoutInflater.from(context);
        circle_anim = AnimationUtils.loadAnimation(context, R.anim.anim_round_rotate);
        LinearInterpolator interpolator = new LinearInterpolator();  //设置匀速旋转，在xml文件中设置会出现卡顿
        circle_anim.setInterpolator(interpolator);
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final RecorderEntity recorderEntity = list.get(position);
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflate.inflate(R.layout.item_recoder_sign, null);
            holder = new ViewHolder(convertView,position);
            convertView.setTag(holder);//绑定ViewHolder对象
        } else {
            holder = (ViewHolder) convertView.getTag();//取出ViewHolder对象
        }
        final EditText editText = (EditText) holder.et_voice_content;
        holder.tv_recorder_sign_time.setText(recorderEntity.dateStr);
        editText.setText(recorderEntity.text);
        if (recorderEntity.isCurrent) {
            editText.setVisibility(View.VISIBLE);
            if (recorderEntity.size <= 0) {
                holder.ll_save.setVisibility(View.VISIBLE);
                holder.ll_save.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onItemClickListener != null) {
                            onItemClickListener.onSaveBtnClick(position, editText.getText().toString());
                        }
                    }
                });
                editText.setFocusable(true);
                editText.setCursorVisible(true);
                editText.setFocusableInTouchMode(true);
            } else {
                holder.ll_save.setVisibility(View.GONE);
                editText.setFocusable(false);
                editText.setCursorVisible(false);
                editText.setFocusableInTouchMode(false);
            }
        } else {
            editText.setVisibility(View.GONE);
            holder.ll_save.setVisibility(View.GONE);
        }
        editText.setText(recorderEntity.text);
        holder.vv_sign.setAUDIO_PATH(recorderEntity.audio_path);
        final ImageView imageView = holder.iv_item_sign_open_edittext;
        final LinearLayout linearLayout = holder.ll_save;
        holder.ll_item_sign_open_edittext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (recorderEntity.text == null || recorderEntity.text.equals("")) {
                    Observable.create(new Observable.OnSubscribe<String>() {
                        @Override
                        public void call(Subscriber<? super String> subscriber) {
                            String str = FileUtil.readFileContentStr(recorderEntity.txt_path + recorderEntity.name);
                            if(SignItemAdapter.this.onItemClickListener!=null){
                                onItemClickListener.changeList(str,position);
                            }
                            subscriber.onNext(str);
                            subscriber.onCompleted();
                        }
                    }).subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(new Observer<String>() {
                                @Override
                                public void onCompleted() {
                                }

                                @Override
                                public void onError(Throwable e) {
                                }

                                @Override
                                public void onNext(String str) {
                                    editText.setText(str);
                                }
                            });
                }
                if (!list.get(position).isCurrent) {
                    editText.setVisibility(View.VISIBLE);
                    linearLayout.setVisibility(View.VISIBLE);
                    imageView.setImageResource(R.drawable.btn_hide_text);
                } else {
                    editText.setVisibility(View.GONE);
                    linearLayout.setVisibility(View.GONE);
                    imageView.setImageResource(R.drawable.btn_text_show);
                }
                list.get(position).isCurrent = !list.get(position).isCurrent;
            }
        });
        holder.vv_sign.setTimeLong(recorderEntity.timelong);
        return convertView;
    }

    class ViewHolder {

        VoiceView vv_sign;
        EditText et_voice_content;
        LinearLayout ll_save,ll_item_sign_open_edittext;
        TextView tv_recorder_sign_time;
        ImageView iv_item_sign_open_edittext;

        ViewHolder(View view,int position) {
            vv_sign = view.findViewById(R.id.vv_sign);
            et_voice_content = view.findViewById(R.id.et_voice_content);
            et_voice_content.setTag(position);
            et_voice_content.addTextChangedListener(new TextSwitcher(this));
            ll_save = view.findViewById(R.id.ll_save);
            ll_item_sign_open_edittext=view.findViewById(R.id.ll_item_sign_open_edittext);
            tv_recorder_sign_time = view.findViewById(R.id.tv_recorder_sign_time);
            iv_item_sign_open_edittext = view.findViewById(R.id.iv_item_sign_open_edittext);
        }
    }

    class TextSwitcher implements TextWatcher {
        private ViewHolder mHolder;

        public TextSwitcher(ViewHolder mHolder) {
            this.mHolder = mHolder;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            int position = (int) mHolder.et_voice_content.getTag();
            if(onItemClickListener!=null){
                onItemClickListener.onEditTextDataChanged(position,s.toString());
            }
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    }
}
